var app = new Vue({
	el: '#app',
	data: {
		id: null,
		panel: 'medicine',
		menu:'medicine',
		isLogin: false,
		account: '',
		password: '',
		accountName: '',
		type: 'clerk',
		oldPassword: '',
		newPassword: '',
		viewBillMsg: {},
		viewMedicineMsg: {},
		medicine: [],
		medicineShow: [],
		medicinePage: 1,
		billShow: null,
		billPage: 1,
		bill: null,
		operateMedicineMsg: {},
		operateAmout: null,
		operateMessage: '',
		operateRemark: '',
		searchMedicineName: '',
		addMedicineMsg: {
			name: null,
			price: null,
			introduce: null,
			imgurl: null
		}
	},
	watch: {
		medicinePage: function(){
			this.renderMedicine();
		},
		billPage: function(){
			this.renderBill();
		}
	},
	methods: {
		getLoginStatus: function(){
			var self = this;
			$.get('/api/isLogin',function(data,status){
				if (data.success == "ok") {
					self.isLogin = true;
					self.accountName = data.result.name;
					self.id = data.result.id;
					if (data.result.type != 'buyer') {
						window.location.href = "/login.html";
					}
					self.getBill();
				} else {
					self.isLogin = false;
					self.accountName = "";
					window.location.href = "/login.html"
				}
			})
		},
		login: function(){
			var self = this;
			$.post("/api/login",
			{
				account: this.account,
				password: this.password,
				type: this.type
			},
			function(data,status){
				if (data.success == "ok") {
					self.getLoginStatus();
				}
			});
		},
		logout: function(){
			var self = this;
			$.post("/api/logout",{},function(data,status){
				if (data.success = "ok") {
					self.getLoginStatus();
				}
			});
		},
		changePassword: function(){
			var self = this;
			$.post("/api/changePassword",
			{
				oldPassword: self.oldPassword,
				newPassword: self.newPassword
			},function(data,status){
				if (data.success == "ok") {
					self.oldPassword = self.newPassword = null;
					$('#passwordModal').modal('hide');
					swal("修改成功", "密码已更新", "success");
				} else {
					swal("修改失败", data.msg, "error");
				}
			});
		},
		changeMenu: function(param){
			this.menu = param;
			this.panel = param;
		},
		getMedicine: function(){
			var self = this;
			$.post("/api/viewMedicine",{},function(data, status){
				if (data.success == "ok") {
					self.medicine = data.result.data;
					self.renderMedicine();
				}
			})
		},
		renderMedicine: function(){
			var self = this;
			var page = self.medicinePage;
			self.medicineShow = [];
			for (var i = 0; i < 20 ; i++) {
				if (self.medicine[(page-1)*20+i] != undefined) {
					self.medicineShow.push(self.medicine[(page-1)*20+i]);
				}
			}
		},
		renderBill: function(){
			var self = this;
			var page = self.billPage;
			self.billShow = [];
			for (var i = 0; i < 20 ; i++) {
				if (self.bill[(page-1)*20+i] != undefined) {
					self.billShow.push(self.bill[(page-1)*20+i]);
				}
			}
		},
		getBill: function(){
			var self = this;
			$.post("/api/viewBill",{"clerk_id": self.id},function(data, status){
				if (data.success == "ok") {
					self.bill = data.result.data;
					self.renderBill();
				}
			})
		},
		changePage: function(type, page){
			this[type+"Page"] = page;
		},
		logout: function(){
			$.post("/api/logout",{},function(data, status){
				if (data.success == "ok") {
					window.location.href = "/login.html"
				} else {
					alert("退出错误");
				}
			})
		},
		viewMedicine: function(msg){
			console.log(msg)
			this.viewMedicineMsg = msg;
			$('#medicineModal').modal('show')
		},
		viewBill: function(msg){
			console.log(msg);
			this.viewBillMsg = msg;
			$('#billModal').modal('show')
		},
		operateMedicine: function(msg){
			console.log(msg);
			this.operateMedicineMsg = msg;
			$('#operateModal').modal('show');
		},
		showChangePassword: function(){
			$('#passwordModal').modal('show');
		},
		operate: function(){
			var self = this;
			var medicine_id = this.operateMedicineMsg.id;
			var type = "purchase";
			var amout = this.operateAmout;
			var message = this.operateMessage;
			var remarks = this.operateRemark;
			if (this.operateMedicineMsg.id == undefined) {
				swal("采购失败", "id信息缺失", "error")
			}else if ( this.operateAmout == undefined) {
				swal("采购失败", "数目不能为空", "error")
			} else {
				$.post("/api/addBill",{
					medicine_id:medicine_id,
					type: type,
					amout:amout,
					message:message,
					remarks:remarks
				},function(data, status){
					if (data.success == "ok") {
						self.operateAmout = self.operateMessage = self.operateRemark = null;
						$('#operateModal').modal('hide');
						swal("操作成功", "", "success");
						self.getMedicine();
						self.getBill();
					} else {
						swal("操作成功", data.msg, "error");
					}
				})
			}
		},
		searchMedicine: function(){
			var self = this;
			$.post("/api/viewMedicine",{
				name: self.searchMedicineName
			},function(data, status){
				if (data.success == "ok") {
					self.medicine = data.result.data;
					self.renderMedicine();
				}
			})
		},
		addMedicine: function(){
			$('#addMedicineModal').modal('show');
		},
		uploadImg: function(){
			var self = this;
			var formData = new FormData($("#uploadForm")[0])
			$.ajax({    
				url: '/api/upload' ,  /*这是处理文件上传的servlet*/  
				type: 'POST',    
				data: formData,    
				async: false,    
				cache: false,    
				contentType: false,    
				processData: false,    
				success: function (result) {    
					/*这是预览图片用的，自己在文件上传表单外添加*/  
					console.log(result);
					self.addMedicineMsg.imgurl = result.file_path;
				},    
				error: function (result) { 
					swal("上传失败","","error");
				}    
			}); 
		},
		addMedicineSave: function(){
			var self = this;
			var name = this.addMedicineMsg.name;
			var price = this.addMedicineMsg.price;
			var introduce = this.addMedicineMsg.introduce;
			var imgurl = this.addMedicineMsg.imgurl;
			var producetime = this.addMedicineMsg.producetime;
			var abletime = this.addMedicineMsg.abletime;
			var company = this.addMedicineMsg.company;
			var address = this.addMedicineMsg.address;
			var phone = this.addMedicineMsg.phone;
			var code = this.addMedicineMsg.code;

			if (name == undefined || name == "") {
				swal("保存失败", "名字不能为空", "error")
			} else if ( price == undefined || price == "") {
				swal("保存失败", "价格不能为空", "error")
			} else if ( introduce == undefined || introduce == "") {
				swal("保存失败", "介绍不能为空", "error")
			} else if ( imgurl == undefined || imgurl == "") {
				swal("保存失败", "图片不能为空", "error")
			} else if ( producetime == undefined || producetime == "") {
				swal("保存失败", "生产日期不能为空", "error")
			} else if ( abletime == undefined || abletime == "") {
				swal("保存失败", "有效期不能为空", "error")
			} else if ( company == undefined || company == "") {
				swal("保存失败", "公司不能为空", "error")
			} else if ( address == undefined || address == "") {
				swal("保存失败", "地址不能为空", "error")
			} else if ( phone == undefined || phone == "") {
				swal("保存失败", "电话不能为空", "error")
			} else if ( code == undefined || code == "") {
				swal("保存失败", "注册代码不能为空", "error")
			} else {

				producetime = (new Date(producetime).valueOf())/1000;
				abletime = Number(abletime)*(30*24*3600);

				var data = {
					name: name,
					price: price,
					introduce: introduce,
					imgurl: imgurl,
					producetime: producetime,
					abletime: abletime,
					company: company,
					address: address,
					phone: phone,
					code: code
				}

				$.post("/api/addMedicine",data,function(data, status){
					if (data.success == "ok") {
						$('#addMedicineModal').modal('hide');
						swal("保存成功", "药品已添加", "success");
						self.getMedicine();
					} else {
						swal("保存失败", data.msg, "error");
					}
				})
			}
		}
	},
	mounted: function(){
		this.getLoginStatus();
		this.getMedicine();
		var self = this;
		$("#inputFile").change(function(){
			self.uploadImg();
		})
	}
})
